#include "stdafx.h"
#include "lens_flare.h"
#include <iltclient.h>
#include "../ltclientshell.h"
#include"../clientinterfaces.h"
#include "../Screen/tex_mgr.h"



lens_flare::lens_flare(void):
m_camera(0),
lens_wide(10.0f),
lens_long(10.0f),
m_tex(0)
{
	for (int i=0;i<8;++i)
	{
		
		for (int j=0;j<4;++j)
		{
			m_poly[i].verts[j].rgba.a=20*i;
			m_poly[i].verts[j].rgba.r=255;
			m_poly[i].verts[j].rgba.g=255-i*30;
			m_poly[i].verts[j].rgba.b=i*30;
		}
		


		m_poly[i].verts[0].u=0.0f;
		m_poly[i].verts[0].v=0.0f;

		m_poly[i].verts[1].u=1.0f;
		m_poly[i].verts[1].v=0.0f;

		m_poly[i].verts[2].u=1.0f;
		m_poly[i].verts[2].v=1.0f;

		m_poly[i].verts[3].u=0.0f;
		m_poly[i].verts[3].v=1.0f;


	}




}

lens_flare::~lens_flare(void)
{

}




void lens_flare::render()
{
	if (!m_tex)
	{
		m_tex=tex_mgr::single().get_tex("Flare2");
	}
	LTMatrix _ma;
	LTRotation _rot;
	LTVector _pos;

	
	g_pLTClient->GetObjectRotation(m_camera,&_rot);
	g_pLTClient->GetObjectPos(m_camera,&_pos);
	_rot.ConvertToMatrix(_ma);
	_ma.Inverse();
	_ma.Apply4x4(m_sun_pos,real_sun_pos);
	

	set_flare_pos();




	g_pLTCDrawPrim->SetTexture(m_tex);
	//g_pLTCDrawPrim->SetCamera(m_camera);
	g_pLTCDrawPrim->SetTransformType(DRAWPRIM_TRANSFORM_CAMERA);
	g_pLTCDrawPrim->SetZBufferMode(DRAWPRIM_ZRW);
	g_pLTCDrawPrim->SetColorOp(DRAWPRIM_MODULATE);
	g_pLTCDrawPrim->DrawPrim(&m_poly[0],7);
	


}

void lens_flare::set_flare_pos()
{

	for (int i=0;i<8;++i)
	{
	

			m_poly[i].verts[0].x=real_sun_pos.x-lens_wide;
			m_poly[i].verts[0].y=real_sun_pos.y+lens_wide;
			m_poly[i].verts[0].z=real_sun_pos.z-lens_long*i;

			m_poly[i].verts[1].x=real_sun_pos.x+lens_wide;
			m_poly[i].verts[1].y=real_sun_pos.y+lens_wide;
			m_poly[i].verts[1].z=real_sun_pos.z-lens_long*i;

			m_poly[i].verts[2].x=real_sun_pos.x+lens_wide;
			m_poly[i].verts[2].y=real_sun_pos.y-lens_wide;
			m_poly[i].verts[2].z=real_sun_pos.z-lens_long*i;

			m_poly[i].verts[3].x=real_sun_pos.x-lens_wide;
			m_poly[i].verts[3].y=real_sun_pos.y-lens_wide;
			m_poly[i].verts[3].z=real_sun_pos.z-lens_long*i;

		
	}



}



